package xml;

import xml.elements.DataItem;
import xml.elements.DataItemInput;
import xml.elements.DataItemSet;
import xml.elements.DataItemSetInput;
import xml.elements.EnumeratedUserInput;
import xml.elements.LogicalOperator;
import xml.elements.LogicalOperatorUserInput;
import xml.elements.OptionalDataItemInput;
import xml.elements.OptionalDataItemSetInput;
import xml.elements.OptionalEnumeratedUserInput;
import xml.elements.OptionalLogicalOperatorUserInput;
import xml.elements.OptionalStructureInput;
import xml.elements.OptionalStructureSetInput;
import xml.elements.Structure;
import xml.elements.StructureInput;
import xml.elements.StructureSet;
import xml.elements.StructureSetInput;
import xml.elements.SubState;
import xml.elements.Text;
import xml.others.FacadeConstructorUID;

public class FactorySubState extends ScannerSubStateAbstract {

	private String idState;
	private String idUID;
	private String idSubState;

	public FactorySubState(String idUID, String idState, SubState stateSource) {
		this.idUID = idUID;
		this.idState = idState;
		this.idSubState = stateSource.getId();
		scanSubState(stateSource);
	}

	@Override
	protected void dataItemFound(DataItem o) {

		FacadeConstructorUID.getInstance().insertOutputDataItemInSubState(
				idUID, idState, idSubState, o.getName(), o.getDomain());
	}

	@Override
	protected void dataItemInputFound(DataItemInput o) {

		FacadeConstructorUID.getInstance().insertInputDataItemInSubState(idUID,
				idState, idSubState, o.getName(), o.getDomain());

	}

	@Override
	protected void enumeratedUserInputFound(EnumeratedUserInput o) {

		FacadeConstructorUID.getInstance().insertInputEnumeratedDataInSubState(
				idUID, idState, idSubState, o.getName(), o.getOptions(),
				o.getMin(), o.getMax());

	}

	@Override
	protected void logicalOperatorUserInputFound(LogicalOperatorUserInput o) {

		String name1, name2, domain1, domain2;

		try {

			String[] a = o.getDataItem1().replaceAll(" ", "").split(":");
			name1 = a[0];
			domain1 = a[1];

		} catch (Exception e) {
			name1 = o.getDataItem1();
			domain1 = null;
		}

		try {

			String[] a = o.getDataItem2().replaceAll(" ", "").split(":");
			name2 = a[0];
			domain2 = a[1];

		} catch (Exception e) {
			name2 = o.getDataItem2();
			domain2 = null;
		}

		if (o.getOperator() == null
				|| o.getOperator().equals(LogicalOperator.XOR)) {
			FacadeConstructorUID.getInstance()
					.insertInputXORDataEntryInSubState(idUID, idState,
							idSubState, name1, domain1, name2, domain2);
		} else {
			FacadeConstructorUID.getInstance()
					.insertInputORDataEntryInSubState(idUID, idState,
							idSubState, name1, domain1, name2, domain2);
		}

	}

	@Override
	protected void optionalDataItemInputFound(OptionalDataItemInput o) {

		FacadeConstructorUID.getInstance()
				.insertInputOptionalDataItemInSubState(idUID, idState,
						idSubState, o.getName(), o.getDomain());

	}

	@Override
	protected void optionalEnumeratedUserInputFound(
			OptionalEnumeratedUserInput o) {

		FacadeConstructorUID.getInstance()
				.insertOptionalInputEnumeratedDataInSubState(idUID, idState,
						idSubState, o.getName(), o.getOptions(), o.getMin(),
						o.getMax());

	}

	@Override
	protected void optionalLogicalOperatorUserInputFound(
			OptionalLogicalOperatorUserInput o) {

		String name1, name2, domain1, domain2;

		try {

			String[] a = o.getDataItem1().replaceAll(" ", "").split(":");
			name1 = a[0];
			domain1 = a[1];

		} catch (Exception e) {
			name1 = o.getDataItem1();
			domain1 = null;
		}

		try {

			String[] a = o.getDataItem2().replaceAll(" ", "").split(":");
			name2 = a[0];
			domain2 = a[1];

		} catch (Exception e) {
			name2 = o.getDataItem2();
			domain2 = null;
		}

		if (o.getOperator() == null
				|| o.getOperator().equals(LogicalOperator.XOR)) {
			FacadeConstructorUID.getInstance()
					.insertInputXORDataEntryInSubState(idUID, idState,
							idSubState, name1, domain1, name2, domain2);
		} else {
			FacadeConstructorUID.getInstance()
					.insertInputORDataEntryInSubState(idUID, idState,
							idSubState, name1, domain1, name2, domain2);
		}

	}

	@Override
	protected void optionalStructureInputFound(OptionalStructureInput o) {

		FacadeConstructorUID.getInstance()
				.insertInputOptionalStructureInSubState(idUID, idState,
						idSubState, o.getName(), o.getData());

	}

	@Override
	protected void structureFound(Structure o) {

		FacadeConstructorUID.getInstance().insertOutputStructureInSubState(
				idUID, idState, idSubState, o.getName(), o.getData());

	}

	@Override
	protected void structureInputFound(StructureInput o) {

		FacadeConstructorUID.getInstance().insertInputStructureInSubState(
				idUID, idState, idSubState, o.getName(), o.getData());

	}

	@Override
	protected void textFound(Text o) {

		FacadeConstructorUID.getInstance().insertTextInSubState(idUID, idState,
				idSubState, o.getId(), o.getValue());

	}

	@Override
	protected void dataItemSetFound(DataItemSet o) {

		FacadeConstructorUID.getInstance().insertOutputSetDataItemInSubState(idUID,
				idState, idSubState, o.getName(), o.getDomain(),o.getMin(), o.getMax());


	}

	@Override
	protected void dataItemSetInputFound(DataItemSetInput o) {

		FacadeConstructorUID.getInstance().insertInputSetDataItemInSubState(idUID,
				idState, idSubState, o.getName(), o.getDomain(),o.getMin(), o.getMax());

	}

	@Override
	protected void optionalDataItemSetInputFound(OptionalDataItemSetInput o) {

		FacadeConstructorUID.getInstance().insertInputOptionalSetDataItemInSubState(idUID,
				idState, idSubState, o.getName(), o.getDomain(),o.getMin(), o.getMax());


	}

	@Override
	protected void optionalStructureSetInputFound(OptionalStructureSetInput o) {

		FacadeConstructorUID.getInstance().insertInputOptionalSetStructureInSubState(
				idUID, idState, idSubState, o.getName(), o.getData(),
				o.getMin(), o.getMax());

	}

	@Override
	protected void structureSetFound(StructureSet o) {
		FacadeConstructorUID.getInstance().insertOutputSetStructureInSubState(
				idUID, idState, idSubState, o.getName(), o.getData(),
				o.getMin(), o.getMax());

	}

	@Override
	protected void structureSetInputFound(StructureSetInput o) {
		
		FacadeConstructorUID.getInstance().insertInputSetStructureInSubState(
				idUID, idState, idSubState, o.getName(), o.getData(),
				o.getMin(), o.getMax());

	}

}
